import { get, post, patch, del } from './fetch'

import { getCourt } from '@/lib/courtsService';
import { getCampusInfo } from '@/lib/campusService';
/**
 * 创建可预约时间段
 * @param {Object} timeblock 时间段数据
 * @returns {Promise<Object>} 新创建的时间段
 */
export async function createBookableTimeblock(timeblock) {
  'use server'
  try {
    console.log('createBookableTimeblock', timeblock);
    const newTimeblock = await post('/api/v1/court-bookable-timeblocks', timeblock);
    return newTimeblock;
  } catch (error) {
    throw error;
  }
}

/**
 * 批量创建可预约时间段
 * @param {Object} data 包含timeblocks数组的对象
 * @returns {Promise<Array>} 新创建的时间段数组
 */
export async function createBookableTimeblocks(data) {
  'use server'
  try {
    const newTimeblocks = await post('/api/v1/court-bookable-timeblocks/batch', data);
    return newTimeblocks;
  } catch (error) {
    throw error;
  }
}

/**
 * 获取可预约时间段列表
 * @param {Object} params 查询参数
 * @returns {Promise<Array>} 时间段列表
 */
export async function getBookableTimeblocks({ court_id,  start_time_from, end_time_to, status }) {
  'use server'
  try {
    console.log("获取可预约时间段列表",{ court_id, start_time_from, end_time_to, status });
    const timeblocks = await get('/api/v1/court-bookable-timeblocks', {
      court_id,
      start_time: start_time_from,
      end_time: end_time_to,
      status
    });
    return timeblocks;
  } catch (error) {
    throw error;
  }
}

/**
 * 更新可预约时间段
 * @param {string} id 时间段ID
 * @param {Object} timeblock 时间段数据
 * @returns {Promise<Object>} 更新后的时间段
 */
export async function updateBookableTimeblock(id, timeblock) {
  'use server'
  try {
    const updatedTimeblock = await patch(`/api/v1/court-bookable-timeblocks/${id}`, timeblock);
    return updatedTimeblock;
  } catch (error) {
    throw error;
  }
}

/**
 * 删除可预约时间段
 * @param {string} id 时间段ID
 * @returns {Promise<void>}
 */
export async function deleteBookableTimeblock(id) {
  'use server'
  try {
    await del(`/api/v1/court-bookable-timeblocks/${id}`);
  } catch (error) {
    throw error;
  }
}

/**
 * 创建场地预约订单
 * @param {Object} order 订单数据
 * @returns {Promise<Object>} 新创建的订单
 */
export async function createBookOrder(order) {
  'use server'
  try {
    const newOrder = await post('/api/v1/court-book-orders', order);
    return newOrder;
  } catch (error) {
    throw error;
  }
}

/**
 * 获取用户的场地预约订单
 * @param {Object} params 查询参数
 * @returns {Promise<Array>} 订单列表
 */
export async function getUserBookOrders({ user_id, status }) {
  'use server'
  try {
    const orders = await get('/api/v1/court-book-orders', {
      user_id,
      status
    });
    // enrich orders with court_name,campus_address,principal_name,principal_phone_number
    // const enrichedOrders = await Promise.all(orders.map(async (order) => {
    //   const court = await getCourt(order.court_id);
    //   const campus = await getCampusInfo(court.branch_campus_id);
    //   return { ...order,  campus_name: campus.name, campus_address: campus.address, campus_phone_number: campus.phone_number, principal_name: campus.principal, };
    // }));
    // return enrichedOrders;
    return orders;
  } catch (error) {
    throw error;
  }
}

/**
 * 通过 out_trade_no 获取订单
 * @param {string} out_trade_no 订单号
 * @returns {Promise<Object>} 订单
 */
export async function getOrderForOutTradeNo(out_trade_no) {
  'use server'
  try {
    const order = await get(`/api/v1/court-book-orders/by-trade-no/${out_trade_no}`);
    return order;
  } catch (error) {
    throw error;
  }
}

/**
 * 更新订单支付状态
 * @param {string} id 订单ID
 * @param {Object} data 支付数据
 * @returns {Promise<Object>} 更新后的订单
 */
export async function updateOrderPayment(id, data) {
  'use server'
  try {
    const updatedOrder = await patch(`/api/v1/court-book-orders/${id}/payment`, data);
    return updatedOrder;
  } catch (error) {
    throw error;
  }
}

/**
 * 通过 out_trade_no 更新订单
 * @param {string} out_trade_no 订单号
 * @param {Object} data 支付数据
 * @returns {Promise<Object>} 更新后的订单
 */
export async function updateOrderPaymentByOutTradeNo(out_trade_no, data) {
  'use server'
  try {
    const updatedOrder = await patch(`/api/v1/court-book-orders/by-trade-no/${out_trade_no}`, data);
    return updatedOrder;
  } catch (error) {
    throw error;
  }
}

/**
 * 取消订单
 * @param {string} id 订单ID
 * @returns {Promise<void>}
 */
export async function cancelBookOrder(id) {
  'use server'
  try {
    await del(`/api/v1/court-book-orders/${id}`);
  } catch (error) {
    throw error;
  }
}

/**
 * 获取时间段关联的订单
 * @param {string} id 时间段ID
 * @returns {Promise<Object>} 关联的订单
 */
export async function getOrderForTimeblock(id) {
  'use server'
  try {
    const order = await get(`/api/v1/court-bookable-timeblocks/${id}/order`);
    return order;
  } catch (error) {
    throw error;
  }
}

/**
 * 删除订单
 * step1 通过block_id 获取订单
 * step2 删除订单
 * @param {string} id 订单ID
 * @returns {Promise<void>}
 */
export async function deleteOrderByBlockId(block_id) {
  'use server'
  try {
    const order = await getOrderForTimeblock(block_id);
    await cancelBookOrder(order.id);
  } catch (error) {
    throw error;
  }
}